/**
 * Created by yuweimei on 2018/4/19.
 */
app.controller('mycollection', function($scope, $http) {

	//显示删除操作页面
	$scope.allShow1 = function(tourId) {
		$scope.tourId = tourId;
		$('.all').css("display", "block");
	};
	//隐藏删除操作页面
	$scope.allHide1 = function() {
		$('.all').css("display", "none");
	};

	$('.alert_top').click(function() {
		$('.all').css("display", "none");
	});
	//证书
	var token = localStorage.getItem('token');
	$scope.collectionList = [];
	//查询登录用户点赞动态列表
	$scope.tourIdLikeList = [];
	$scope.mycollectionInit = function() {
		if(!token) {
			//判断有无证书
			$.toast("请先登录");
			location.href = '#login';
			return;
		} else {
			//点赞动态列表id
			$http({
				method: 'get',
				url: app.netPath + '/dynamic/details/show/likes',
				headers: {
					'Authorization': token
				}
			}).then(function(res) {
				if(!res.data.error) {
					for(var i = 0; i < res.data.data.length; i++) {
						$scope.tourIdLikeList.push(res.data.data[i].likedynaId);
					}
					//判断有无证书
					if(!token) {
						$.toast("请先登录");
						location.href = '#login';
						return;
					}
					$http({
						method: 'get',
						url: app.netPath + '/collection/show',
						headers: {
							'Authorization': token
						}
					}).then(function(res) {
						if(!res.data.error) {
							for(var i = 0; i < res.data.data.length; i++) {
								if(res.data.data[i].tourDynamics.user.userAvatarAddress) {
									res.data.data[i].tourDynamics.user.userAvatarAddress = app.netPath + '/resources/img/' + res.data.data[i].tourDynamics.user.userAvatarAddress;
									app.buildImageUrl(res.data.data[i].tourDynamics.user, 'userAvatarAddress', function() {
										$scope.$digest();
									});
								}
								//放置动态图片
								if(res.data.data[i].tourDynamics.tourdynaImg) {
									//得到每条动态的图片数组
									var dynamicImgs = app.getDynamicImg(res.data.data[i].tourDynamics.tourdynaImg);
									for(var j = 0; j < dynamicImgs.length; j++) {
										dynamicImgs[j] = app.netPath + '/resources/taskImg/' + dynamicImgs[j];
										app.buildImageUrl(dynamicImgs, j, function() {
											$scope.$digest();
										});
									}
									res.data.data[i].tourDynamics.tourdynaImg = dynamicImgs;
								}
								for(var a = 0; a < $scope.tourIdLikeList.length; a++) {
									if(res.data.data[i].tourDynamics.tourdynaId == $scope.tourIdLikeList[a]) {
										res.data.data[i].tourDynamics.isLike = 1;
									} else {

									}
								}
								var lat = res.data.data[i].tourDynamics.positionX;
								var lng = res.data.data[i].tourDynamics.positionY;
								res.data.data[i].tourDynamics.distance = app.getGreatCircleDistance(lat, lng);
								if(res.data.data[i].tourDynamics.distance >= 1000) {
									res.data.data[i].tourDynamics.distance = (app.getGreatCircleDistance(lat, lng) / 1000).toFixed(2) + "千米";
								} else {
									res.data.data[i].tourDynamics.distance = app.getGreatCircleDistance(lat, lng) + "米";
								}
								console.log(res.data.data[i].tourDynamics.isLike);
								//将动态逐条存入（dynamicsList）数组
								$scope.collectionList.push(res.data.data[i]);
							}
						} else {
							// $.toast(res.data.error + ',' + res.data.msg);
						}
					}, function() {
						$.toast('网络走丢了！')
					});
				} else {

				}
			}, function() {
				$.toast('网络走丢了！')
			});
		}
	};
	//取消收藏
	$scope.reduceCollection = function() {
		if(!token) {
			//判断有无证书
			$.toast("请先登录");
			location.href = '#login';
			return;
		} else {
			$.confirm('确定取消？', '', function() {
				//欲删除的动态id
				var tourId = $scope.tourId;
				$http({
					method: 'get',
					url: app.netPath + '/collection/delete',
					headers: {
						'Authorization': token
					},
					params: {
						collIds: tourId
					}
				}).then(function(res) {
					if(!res.data.error) {
						$('.all').css("display", "none");
						//刷新收藏列表
						$scope.collectionList = [];
						$.toast('已取消收藏');
						$scope.mycollectionInit();

					} else {
						$.toast(res.data.error + ',' + res.data.msg);
					}
				}, function() {
					$.toast('网络走丢了！')
				});
			}, function() {
				$('.all').css("display", "none");
				$.toast('已取消');
			});
		}
	};
	//点赞
	$scope.addLike = function(tourdynaId) {
		$http({
			method: 'get',
			url: app.netPath + '/dynamic/details/likeAdd',
			headers: {
				'Authorization': token
			},
			params: {
				tourdynaId: tourdynaId
			}
		}).then(function(res) {
			if(!res.data.error) {
				$.toast('点赞成功');
				$scope.collectionList = [];
				//刷新页面
				$scope.mycollectionInit();
			} else {
				$.toast(res.data.error + ',' + res.data.msg);

			}
		}, function() {
			$.toast('网络走丢了！')
		});
	};
	$scope.GoToReview = function(tourdynaId) {
		sessionStorage.setItem('tourdynaInfoId', tourdynaId);
	};

});